package xyz.bali16.module.advise.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import lombok.ToString;

import java.util.List;

/**
 * @author bali2 on 9/10/2021.
 * @version 1.0
 */
@Data
@ToString
public class PageUtils {


    /**
     * 每页记录数量
     */
    private Integer pageSize;

    /**
     * 总记录数
     */
    private Integer totalCount;

    /**
     * 总页数
     */
    private Integer totalPage;

    /**
     * 当前所在页
     */
    private Integer currPage;

    private Integer prePage;

    private Integer nextPage;
    /**
     * 列表数据
     */
    private List<?> list;

    private boolean hasPreviousPage;

    private boolean hasNextPage;

    //    /**
//     * 分页
//     *
//     * @param list       列表数据
//     * @param totalCount 总记录数
//     * @param pageSize   每页记录数
//     * @param currPage   当前页数
//     */
    public PageUtils(Integer currPage, Integer pageSize, Integer totalCount, List<?> list) {
        if (pageSize <= 0) {
            pageSize = 1;
        }
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.currPage = currPage;
        this.list = list;
        if (totalCount < pageSize) {
            this.totalPage = 1;
        } else {
            System.out.println("MAth:" + Math.ceil((float)totalCount / (float)pageSize));

            this.totalPage = (int) Math.ceil((float)totalCount / (float)pageSize); //ceil 返回最小,最接近负无穷的值
        }
        this.init();
    }

    //给sql分页用
    public PageUtils(Integer currPage, Integer pageSize, List<?> list) {
        if (pageSize <= 0) {
            pageSize = 1;
        }
        this.pageSize = pageSize;
        this.totalCount = list.size();
        this.currPage = currPage;
        this.list = list;
        if (totalCount < pageSize) {
            this.totalPage = 1;
        } else {
            this.totalPage = (int) Math.ceil((float)totalCount / (float)pageSize); //ceil 返回最小,最接近负无穷的值
        }
        this.init();
    }

    //给mybatis的queryPage用
    public PageUtils(Integer currPage, Integer pageSize, List<?> list, int total) {
        if (pageSize <= 0) {
            pageSize = 1;
        }
        this.pageSize = pageSize;
        this.totalCount = total;
        this.currPage = currPage;
        this.list = list;
        if (totalCount < pageSize) {
            this.totalPage = 1;
        } else {
            this.totalPage = (int) Math.ceil((float)totalCount / (float)pageSize); //ceil 返回最小,最接近负无穷的值
        }
        this.init();
    }


    /**
     * 传入分页 Page 对象接口来初始化分页工具类
     */
    public PageUtils(IPage<?> page) {
        this.list = page.getRecords();
        this.totalCount = (int) page.getTotal();
        this.pageSize = (int) page.getSize();
        this.currPage = (int) page.getCurrent();
        this.totalPage = (int) page.getPages();
        this.init();
    }

    private void init() {
        hasPreviousPage = this.currPage > 1;
        hasNextPage = this.currPage < this.totalPage;
        if (this.currPage > 1) {
            prePage = this.currPage - 1;
        }
        if (this.currPage < this.totalPage) {
            nextPage = this.currPage + 1;
        }
    }

}

